﻿namespace Jhong.FunMVCBlog.Core.Data
{
    using Entity;
    using Enum;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;
    using System.Text;

    public class LogData : BaseData
    {
        public void AddLog(LogType type, LogFrom form, Exception ex)
        {
            this.AddLog(type, form, ex, string.Empty, string.Empty, string.Empty);
        }

        public void AddLog(LogType type, LogFrom form, Exception ex, string url = "", string method = "", string ip = "")
        {
            var model = new Log()
            {
                Content = ex.ToString(),
                CreateTime = DateTime.Now,
                HasRead = HasRead.No,
                IP = string.IsNullOrWhiteSpace(ip) ? string.Empty : ip,
                LogForm = form,
                LogType = type,
                RequestMethod = string.IsNullOrWhiteSpace(method) ? RequestMethod.NOLOG : (RequestMethod)System.Enum.Parse(typeof(RequestMethod), method.ToUpper()),
                URL = string.IsNullOrWhiteSpace(url) ? string.Empty : url
            };
            using (this._dbContext)
            {
                this._dbContext.InsertEntity(model);
            }
        }

        public PageContainer<Log> GetList(int pageIndex, int itemsPerPage, DateTime? startTime, DateTime? endTime, string logType, string logFrom)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (itemsPerPage < 1) itemsPerPage = 10;
            var sb = new StringBuilder();
            var sqlstr = new StringBuilder();
            var countstr = new StringBuilder();
            sqlstr.Append("select ID,CreateTime,URL,IP,HasRead from log where 1=1");
            countstr.Append("select count(*) from log where 1=1");
            if (null != startTime)
            {
                sqlstr.Append(string.Format(" and CreateTime>='{0}' ", startTime.Value.ToShortDateString()));
                countstr.Append(string.Format(" and CreateTime>='{0}' ", startTime.Value.ToShortDateString()));
            }
            if (null != endTime)
            {
                sqlstr.Append(string.Format(" and CreateTime<='{0}' ", endTime.Value.ToShortDateString()));
                countstr.Append(string.Format(" and CreateTime<='{0}' ", endTime.Value.ToShortDateString()));
            }
            if (false == string.IsNullOrWhiteSpace(logType))
            {
                var lt = Enum.Parse(typeof(LogType), logType);
                sqlstr.Append(string.Format(" and LogType={0} ", (int)lt));
                countstr.Append(string.Format(" and LogType={0} ", (int)lt));
            }
            if (false == string.IsNullOrWhiteSpace(logFrom))
            {
                var lf = Enum.Parse(typeof(LogFrom), logFrom);
                sqlstr.Append(string.Format(" and LogForm={0} ", (int)lf));
                countstr.Append(string.Format(" and LogForm={0} ", (int)lf));
            }
            sqlstr.Append(string.Format(" order by ID desc limit {0},{1} ", (pageIndex - 1) * itemsPerPage, itemsPerPage));
            using (this._dbContext)
            {
                var items = this._dbContext.T(sqlstr.ToString()).QueryToEntitys<Log>();
                var totalCount = this._dbContext.T(countstr.ToString()).QueryFirstOrDefault<int>();
                var container = new PageContainer<Log>(pageIndex, itemsPerPage, totalCount, items);
                return container;
            }
        }

        public Log GetByID(int id)
        {
            using (this._dbContext)
            {
                return this._dbContext.T("select * from log where ID=@0", id).QueryFirstOrDefault<Log>();
            }
        }

        public void SetRead(string ids)
        {
            using (this._dbContext)
            {
                this._dbContext.T("UPDATE log SET HasRead=1 WHERE ID in(" + ids + ")").Execute();
            }
        }
    }
}